import base64
import hashlib
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes

def encrypt_with_aes(taskjson, secret_key):
    # 将输入数据转换为 UTF-8 编码的字节序列
    data = taskjson.encode('utf-8')

    # 设置加密参数
    iv = "czxvzcvaewfdfa23".encode('utf-8')  # 生成随机的初始化向量 (IV)
    
    # 使用 SHA-256 对 secret_key 进行哈希以确保其长度适合 AES 密钥
    key = hashlib.sha256(secret_key.encode('utf-8')).digest()

    # 创建 AES Cipher 对象
    cipher = AES.new(key, AES.MODE_CBC, iv)

    # 执行 AES 加密
    encrypted_data = cipher.encrypt(pad(data, AES.block_size))

    # 将 IV 和加密后的数据组合起来，以便解密时使用
    combined_iv_encrypted = iv + encrypted_data

    # 返回加密后的数据（Base64 编码并转为大写）
    return base64.b64encode(combined_iv_encrypted).decode('utf-8').upper()

# 示例用法
taskjson = '{"inputs":[{"files":["E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"],"name":"","srs":"","srsorigin":"0,0,0","towgs84":"","height":10,"heightFactor":1,"bottom":0,"bottomFactor":1,"ceilingHeight":false,"encodingGBK":false,"onlyOutter":false,"simply":0,"buffer":0,"textures":[],"terrains":[],"type":"SHP","dracoCompression":false,"DracoAttributeQuantization":{"position":14,"tex_coord":12,"normal":10,"color":8,"genericAtt":16},"bottomFace":true,"forceDoubleSided":true,"nolight":false,"alphaMode":"","fields":[{"name":"建筑顶","type":"double"},{"name":"建筑底","type":"double"},{"name":"建筑高","type":"double"},{"name":"建筑层","type":"int32_t"},{"name":"x","type":"string"},{"name":"y","type":"string"},{"name":"结构","type":"string"},{"name":"uuid","type":"string"},{"name":"面积","type":"double"},{"name":"sqjcwhmc","type":"string"}],"heightField":"建筑高"}],"database":{"sqlite":{"file":""},"skipImport":false},"nolod":{"cmptMaxSize":2000000,"b3dmMaxSize":20000000},"i3dm":{"minRefCount":50},"lod":{"minZoom":16,"maxZoom":20,"threadcount":4,"filterPixelSize":25,"maxZoomRecon":false,"maxOpaqueTextureSize":1024,"maxTransparentTextureSize":256,"recon":{"enable":true,"keepSrcMesh":false}},"filters":{"simplify":{"exclude":true},"textureproj":{"exclude":true},"lod":{"exclude":true}},"color2texture":false,"dracoCompression":false,"compressLevel":1,"DracoAttributeQuantization":{"position":14,"tex_coord":12,"normal":10,"color":8,"genericAtt":16},"mergeRepeat":false,"textureFormat":"jpgorpng","modelname":false,"packUnselectedProperty":false,"props":[{"name":"uuid","type":"string","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"建筑高","type":"double","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"}],"firstsrs":true,"output":{"path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\白膜\\wuli","type":"file","outPutb3dm":true},"scenetree":true,"fields":[{"name":"建筑顶","type":"double","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"建筑底","type":"double","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"建筑高","type":"double","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"建筑层","type":"int32_t","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"x","type":"string","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"y","type":"string","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"结构","type":"string","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"uuid","type":"string","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"面积","type":"double","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"},{"name":"sqjcwhmc","type":"string","path":"E:\\MyDocument\\ArcGis\\DT\\基地面修改\\0116\\龙坪镇\\五里村\\五里村_白膜.shp"}]}'

secret_key = "rwe23adasfadfaed"

encrypted_data = encrypt_with_aes(taskjson, secret_key)
print(encrypted_data)